{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "65f32ef2",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3f0a02cc",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8a601de4",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "os.chdir('C:/Users/MI/OneDrive/桌面/作业资料下载/作业资料下载')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e687fca3",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "credit_data=pd.read_csv('./车贷违约预测.csv',encoding=\"gbk\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "57553514",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "create_col={\"受否填写身份证\":\"是否填写身份证\",\n",
    "            \"次账户没用还款\":\"次账户每月还款\",\n",
    "            \"骑车销售商\":\"汽车销售商\"\n",
    "           }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "92c7efc5",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "credit_data.rename(columns=create_col,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9c7e79f0",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>汽车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "      <th>...</th>\n",
       "      <th>尚未还清有效贷款总额</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比列</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601758</td>\n",
       "      <td>65532</td>\n",
       "      <td>78990</td>\n",
       "      <td>84.38</td>\n",
       "      <td>136</td>\n",
       "      <td>20490</td>\n",
       "      <td>45</td>\n",
       "      <td>1981</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>519488</td>\n",
       "      <td>56759</td>\n",
       "      <td>65325</td>\n",
       "      <td>89.55</td>\n",
       "      <td>61</td>\n",
       "      <td>22778</td>\n",
       "      <td>86</td>\n",
       "      <td>1967</td>\n",
       "      <td>2018</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>2054139</td>\n",
       "      <td>2036500</td>\n",
       "      <td>2036500</td>\n",
       "      <td>34455</td>\n",
       "      <td>0.99</td>\n",
       "      <td>59</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>447579</td>\n",
       "      <td>58413</td>\n",
       "      <td>67960</td>\n",
       "      <td>89.02</td>\n",
       "      <td>5</td>\n",
       "      <td>15663</td>\n",
       "      <td>86</td>\n",
       "      <td>1977</td>\n",
       "      <td>2018</td>\n",
       "      <td>9</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>648134</td>\n",
       "      <td>72317</td>\n",
       "      <td>99750</td>\n",
       "      <td>73.68</td>\n",
       "      <td>76</td>\n",
       "      <td>17242</td>\n",
       "      <td>48</td>\n",
       "      <td>1995</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>13813</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>13814.00</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>458210</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450</td>\n",
       "      <td>79.45</td>\n",
       "      <td>146</td>\n",
       "      <td>14181</td>\n",
       "      <td>45</td>\n",
       "      <td>1974</td>\n",
       "      <td>2018</td>\n",
       "      <td>17</td>\n",
       "      <td>...</td>\n",
       "      <td>467161</td>\n",
       "      <td>550000</td>\n",
       "      <td>550000</td>\n",
       "      <td>12863</td>\n",
       "      <td>1.18</td>\n",
       "      <td>42</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.06</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 49 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     客户编号   已发货款   资产成本  贷款与资产比列   品牌  汽车销售商  车厂  出生日期  货款日期  地区  ...  \\\n",
       "0  601758  65532  78990    84.38  136  20490  45  1981  2018   8  ...   \n",
       "1  519488  56759  65325    89.55   61  22778  86  1967  2018   6  ...   \n",
       "2  447579  58413  67960    89.02    5  15663  86  1977  2018   9  ...   \n",
       "3  648134  72317  99750    73.68   76  17242  48  1995  2018   8  ...   \n",
       "4  458210  50078  65450    79.45  146  14181  45  1974  2018  17  ...   \n",
       "\n",
       "   尚未还清有效贷款总额  已批准贷款总额  已发放贷款总额  每月还款总额  贷款与已还贷款比列  主账户还款期数  次账户还款期数  \\\n",
       "0           0        0        0       0       1.00        0        0   \n",
       "1     2054139  2036500  2036500   34455       0.99       59        0   \n",
       "2           0        0        0       0       1.00        0        0   \n",
       "3           0    13813    13813       0   13814.00    13813        0   \n",
       "4      467161   550000   550000   12863       1.18       42        0   \n",
       "\n",
       "   贷款与已批准贷款比列  总贷款次数与总有效贷款次数比  工作类型  \n",
       "0         1.0            1.00     0  \n",
       "1         1.0            1.33     1  \n",
       "2         1.0            1.00     1  \n",
       "3         1.0            2.00     0  \n",
       "4         1.0            1.06     1  \n",
       "\n",
       "[5 rows x 49 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "credit_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "016404fe",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 理解数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aef52b04",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 导入字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9c62fee8",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "feature=pd.read_excel('./车贷违约预测字典.xlsx',sheet_name=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3a8ae8df",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "#基本信息\n",
    "def baseinfo(data):\n",
    "    for col in data.columns:\n",
    "        print(\"---------------------\")\n",
    "        print(\"列名:\"+col)\n",
    "        print(\"---------------------\")\n",
    "        print(\"当前数据类型\",data[col].dtype)\n",
    "        print(\"------------------------------------\")\n",
    "        print(data[col].value_counts())\n",
    "        print(\"------------------------------------\")\n",
    "        print(\"数据值数量\",data[col].nunique())\n",
    "        print(\"------------------------------------\")\n",
    "        print(\"是否负值\",data[data[col]<0][col].sum())\n",
    "        print(\"------------------------------------\")\n",
    "        print(\"是否有无限值\",np.isinf(data[col]).any())\n",
    "        print(\"------------------------------------\")\n",
    "        print(\"数据缺失值{}\".format(data[col].isna().sum()))\n",
    "        print(\"------------------------------------\\n\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3aa403a7",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 用户信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f460bbdc",
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------------\n",
      "列名:客户编号\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "524288    1\n",
      "549593    1\n",
      "475837    1\n",
      "481982    1\n",
      "479935    1\n",
      "         ..\n",
      "486599    1\n",
      "464072    1\n",
      "466121    1\n",
      "459978    1\n",
      "526335    1\n",
      "Name: 客户编号, Length: 199717, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 199717\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:工作类型\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0    108941\n",
      "1     84195\n",
      "2      6581\n",
      "Name: 工作类型, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 3\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:出生日期\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "1995    9039\n",
      "1994    9037\n",
      "1992    8854\n",
      "1996    8715\n",
      "1990    8590\n",
      "1993    8548\n",
      "1991    8068\n",
      "1988    7902\n",
      "1989    7618\n",
      "1987    7400\n",
      "1986    7263\n",
      "1985    6799\n",
      "1984    6362\n",
      "1997    6328\n",
      "1983    6151\n",
      "1982    6010\n",
      "1980    5807\n",
      "1981    5289\n",
      "1978    5104\n",
      "1979    4859\n",
      "1975    4841\n",
      "1976    4798\n",
      "1977    4574\n",
      "1974    3996\n",
      "1973    3836\n",
      "1972    3741\n",
      "1970    3483\n",
      "1971    3163\n",
      "1969    2738\n",
      "1968    2628\n",
      "1967    2129\n",
      "1965    2052\n",
      "1966    1962\n",
      "1998    1874\n",
      "1964    1519\n",
      "1999    1385\n",
      "1963    1326\n",
      "1962    1266\n",
      "1960    1061\n",
      "1961    1012\n",
      "1959     710\n",
      "2000     533\n",
      "1958     530\n",
      "1957     350\n",
      "1956     300\n",
      "1955     147\n",
      "1954      19\n",
      "1949       1\n",
      "Name: 出生日期, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 48\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:地区\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "4     38455\n",
      "3     29450\n",
      "6     28799\n",
      "13    15055\n",
      "9     13599\n",
      "8     12146\n",
      "5      8720\n",
      "14     7917\n",
      "1      7748\n",
      "7      5808\n",
      "11     5793\n",
      "18     4665\n",
      "15     4403\n",
      "12     3570\n",
      "2      3505\n",
      "17     3388\n",
      "10     3127\n",
      "16     2322\n",
      "19      889\n",
      "20      164\n",
      "21      131\n",
      "22       63\n",
      "Name: 地区, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 22\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:是否填写手机号\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "1    199717\n",
      "Name: 是否填写手机号, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 1\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:是否填写身份证\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "1    199717\n",
      "Name: 是否填写身份证, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 1\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:是否出具驾驶证\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0    195054\n",
      "1      4663\n",
      "Name: 是否出具驾驶证, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 2\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:是否填写护照\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0    199289\n",
      "1       428\n",
      "Name: 是否填写护照, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 2\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:信用评分\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0      99692\n",
      "738     7419\n",
      "300     7354\n",
      "825     6402\n",
      "15      3192\n",
      "       ...  \n",
      "863        1\n",
      "822        1\n",
      "834        1\n",
      "884        1\n",
      "837        1\n",
      "Name: 信用评分, Length: 572, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 572\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "userinfo=feature.变量名[:9]\n",
    "baseinfo(credit_data[userinfo])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bcc1485",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 汽车信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4da369d0",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------------\n",
      "列名:贷款与资产比列\n",
      "---------------------\n",
      "当前数据类型 float64\n",
      "------------------------------------\n",
      "85.00    3656\n",
      "84.99     861\n",
      "79.99     448\n",
      "80.00     408\n",
      "79.90     351\n",
      "         ... \n",
      "90.22       1\n",
      "28.72       1\n",
      "92.79       1\n",
      "24.93       1\n",
      "26.43       1\n",
      "Name: 贷款与资产比列, Length: 6457, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 6457\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:货款日期\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "2018    199717\n",
      "Name: 货款日期, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 1\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:对接员工编号\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "2546    478\n",
      "620     419\n",
      "255     402\n",
      "2153    346\n",
      "130     340\n",
      "       ... \n",
      "3780      1\n",
      "3772      1\n",
      "241       1\n",
      "2875      1\n",
      "3764      1\n",
      "Name: 对接员工编号, Length: 3257, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 3257\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:品牌\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "2      11322\n",
      "67      9761\n",
      "3       8040\n",
      "5       7791\n",
      "36      7461\n",
      "       ...  \n",
      "217      156\n",
      "261      146\n",
      "84       131\n",
      "111       78\n",
      "158       53\n",
      "Name: 品牌, Length: 82, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 82\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:汽车销售商\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "18317    1191\n",
      "17980    1083\n",
      "14234    1064\n",
      "15694    1064\n",
      "15663    1061\n",
      "         ... \n",
      "24546       1\n",
      "23741       1\n",
      "22845       1\n",
      "24761       1\n",
      "15045       1\n",
      "Name: 汽车销售商, Length: 2935, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 2935\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:车厂\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "86     94164\n",
      "45     48370\n",
      "51     23356\n",
      "48     14096\n",
      "49      8760\n",
      "120     8257\n",
      "67      2034\n",
      "145      664\n",
      "153       10\n",
      "152        5\n",
      "156        1\n",
      "Name: 车厂, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 11\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:已发货款\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "48349    1845\n",
      "53303    1805\n",
      "51303    1691\n",
      "50303    1673\n",
      "55259    1622\n",
      "         ... \n",
      "21930       1\n",
      "54586       1\n",
      "50488       1\n",
      "55381       1\n",
      "28686       1\n",
      "Name: 已发货款, Length: 22536, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 22536\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:资产成本\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "68000     575\n",
      "67000     503\n",
      "72000     474\n",
      "70000     432\n",
      "66000     407\n",
      "         ... \n",
      "58969       1\n",
      "130100      1\n",
      "134660      1\n",
      "103927      1\n",
      "53222       1\n",
      "Name: 资产成本, Length: 43627, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 43627\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "carinfo=feature.变量名[9:17]\n",
    "baseinfo(credit_data[carinfo])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6337354",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 总贷款信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9970900f",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------------\n",
      "列名:平均贷款期限\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0      101753\n",
      "1       10859\n",
      "13       7267\n",
      "6        5181\n",
      "7        4640\n",
      "        ...  \n",
      "112        10\n",
      "114        10\n",
      "104         9\n",
      "108         6\n",
      "116         6\n",
      "Name: 平均贷款期限, Length: 100, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 100\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:第一次贷款距今时间\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0      101552\n",
      "13       7091\n",
      "25       6391\n",
      "1        5847\n",
      "6        4112\n",
      "        ...  \n",
      "110        75\n",
      "116        75\n",
      "114        72\n",
      "117        70\n",
      "113        66\n",
      "Name: 第一次贷款距今时间, Length: 100, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 100\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:贷款查询次数\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0     173239\n",
      "1      18972\n",
      "2       4608\n",
      "3       1469\n",
      "4        645\n",
      "5        271\n",
      "6        196\n",
      "7        114\n",
      "8         78\n",
      "9         35\n",
      "10        30\n",
      "12        13\n",
      "11        13\n",
      "14         6\n",
      "15         5\n",
      "13         4\n",
      "17         4\n",
      "18         4\n",
      "19         4\n",
      "16         3\n",
      "20         1\n",
      "22         1\n",
      "23         1\n",
      "28         1\n",
      "Name: 贷款查询次数, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 24\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:贷款总次数\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0      98703\n",
      "1      29903\n",
      "2      16904\n",
      "3      11335\n",
      "4       8177\n",
      "       ...  \n",
      "85         1\n",
      "453        1\n",
      "83         1\n",
      "82         1\n",
      "124        1\n",
      "Name: 贷款总次数, Length: 106, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 106\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:无效贷款总次数\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0      128291\n",
      "1       26456\n",
      "2       13077\n",
      "3        7873\n",
      "4        5298\n",
      "        ...  \n",
      "68          1\n",
      "60          1\n",
      "129         1\n",
      "146         1\n",
      "249         1\n",
      "Name: 无效贷款总次数, Length: 97, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 97\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:贷款与已还贷款比列\n",
      "---------------------\n",
      "当前数据类型 float64\n",
      "------------------------------------\n",
      " 1.00       122309\n",
      " 1.02         1075\n",
      " 1.01         1061\n",
      " 1.05         1039\n",
      " 1.04         1029\n",
      "             ...  \n",
      " 14.12           1\n",
      " 1148.00         1\n",
      " 90.03           1\n",
      "-17.59           1\n",
      " 77.67           1\n",
      "Name: 贷款与已还贷款比列, Length: 5211, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 5211\n",
      "------------------------------------\n",
      "是否负值 -813356.93\n",
      "------------------------------------\n",
      "是否有无限值 True\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:贷款与已批准贷款比列\n",
      "---------------------\n",
      "当前数据类型 float64\n",
      "------------------------------------\n",
      "1.00         189351\n",
      "0.99            864\n",
      "1.01            711\n",
      "0.98            588\n",
      "0.97            462\n",
      "              ...  \n",
      "17001.00          1\n",
      "45001.00          1\n",
      "2.43              1\n",
      "5.83              1\n",
      "140001.00         1\n",
      "Name: 贷款与已批准贷款比列, Length: 432, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 432\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:总贷款次数与总有效贷款次数比\n",
      "---------------------\n",
      "当前数据类型 float64\n",
      "------------------------------------\n",
      "1.00    115897\n",
      "2.00     26698\n",
      "1.50     10433\n",
      "3.00      7571\n",
      "1.33      4846\n",
      "         ...  \n",
      "4.60         1\n",
      "5.25         1\n",
      "2.04         1\n",
      "5.75         1\n",
      "3.30         1\n",
      "Name: 总贷款次数与总有效贷款次数比, Length: 222, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 222\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:近六个月新贷款次数\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0     155081\n",
      "1      27757\n",
      "2       9474\n",
      "3       3874\n",
      "4       1679\n",
      "5        839\n",
      "6        413\n",
      "7        270\n",
      "8        128\n",
      "9         71\n",
      "10        49\n",
      "11        23\n",
      "12        16\n",
      "13        13\n",
      "14         9\n",
      "16         5\n",
      "17         5\n",
      "20         3\n",
      "18         2\n",
      "23         2\n",
      "15         1\n",
      "21         1\n",
      "28         1\n",
      "35         1\n",
      "Name: 近六个月新贷款次数, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 24\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:近六个月违约次数\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0     184345\n",
      "1      12655\n",
      "2       2054\n",
      "3        459\n",
      "4        111\n",
      "5         48\n",
      "6         19\n",
      "7         11\n",
      "8          7\n",
      "9          2\n",
      "10         2\n",
      "12         2\n",
      "11         1\n",
      "20         1\n",
      "Name: 近六个月违约次数, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 14\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:尚未还清有效贷款总额\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0          119921\n",
      "800           109\n",
      "400           104\n",
      "30000          86\n",
      "50000          71\n",
      "            ...  \n",
      "4655055         1\n",
      "784337          1\n",
      "143338          1\n",
      "141291          1\n",
      "48808           1\n",
      "Name: 尚未还清有效贷款总额, Length: 63779, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 63779\n",
      "------------------------------------\n",
      "是否负值 -23489561\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:已批准贷款总额\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0          116836\n",
      "50000        1266\n",
      "30000        1229\n",
      "100000        820\n",
      "25000         814\n",
      "            ...  \n",
      "50861           1\n",
      "65194           1\n",
      "2404000         1\n",
      "793488          1\n",
      "156300          1\n",
      "Name: 已批准贷款总额, Length: 40019, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 40019\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:已发放贷款总额\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0         116927\n",
      "50000       1183\n",
      "30000       1133\n",
      "100000       792\n",
      "40000        673\n",
      "           ...  \n",
      "78859          1\n",
      "31778          1\n",
      "29731          1\n",
      "23590          1\n",
      "6183           1\n",
      "Name: 已发放贷款总额, Length: 43135, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 43135\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:每月还款总额\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0        135649\n",
      "1620        239\n",
      "1500        134\n",
      "1600        125\n",
      "2000        117\n",
      "          ...  \n",
      "11207         1\n",
      "21448         1\n",
      "17354         1\n",
      "19403         1\n",
      "22731         1\n",
      "Name: 每月还款总额, Length: 26122, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 26122\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:贷款与资产比\n",
      "---------------------\n",
      "当前数据类型 float64\n",
      "------------------------------------\n",
      "0.761323    133\n",
      "0.751046     61\n",
      "0.717988     57\n",
      "0.720886     50\n",
      "0.759402     46\n",
      "           ... \n",
      "0.854449      1\n",
      "0.774318      1\n",
      "0.747216      1\n",
      "0.694090      1\n",
      "0.715263      1\n",
      "Name: 贷款与资产比, Length: 180402, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 180402\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "totalinfo=feature.变量名[17:32]\n",
    "baseinfo(credit_data[totalinfo])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "95096d22",
   "metadata": {
    "hidden": true,
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      98703\n",
       "1       2672\n",
       "2        265\n",
       "3         63\n",
       "4         22\n",
       "5         10\n",
       "6          4\n",
       "8          4\n",
       "7          2\n",
       "14         2\n",
       "132        1\n",
       "13         1\n",
       "23         1\n",
       "30         1\n",
       "39         1\n",
       "45         1\n",
       "Name: 贷款总次数, dtype: int64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#贷款总次数为0时，平均贷款期限和第一次贷款距今时间也需要同时为0\n",
    "credit_data[credit_data.平均贷款期限==0].贷款总次数.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c15d5a4a",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 主贷款信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d91eb23e",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------------\n",
      "列名:主账户贷款次数\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0      99692\n",
      "1      30024\n",
      "2      16908\n",
      "3      11216\n",
      "4       8060\n",
      "       ...  \n",
      "147        1\n",
      "85         1\n",
      "453        1\n",
      "83         1\n",
      "124        1\n",
      "Name: 主账户贷款次数, Length: 106, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 106\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:主账户有效贷款次数\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0      116995\n",
      "1       36038\n",
      "2       18507\n",
      "3       10585\n",
      "4        6447\n",
      "5        3964\n",
      "6        2422\n",
      "7        1548\n",
      "8        1058\n",
      "9         661\n",
      "10        453\n",
      "11        288\n",
      "12        200\n",
      "13        147\n",
      "14         98\n",
      "15         79\n",
      "16         50\n",
      "17         47\n",
      "18         32\n",
      "19         26\n",
      "20         13\n",
      "21          9\n",
      "23          8\n",
      "24          8\n",
      "22          6\n",
      "25          6\n",
      "28          5\n",
      "26          4\n",
      "31          2\n",
      "32          2\n",
      "34          2\n",
      "144         1\n",
      "43          1\n",
      "52          1\n",
      "27          1\n",
      "42          1\n",
      "37          1\n",
      "65          1\n",
      "Name: 主账户有效贷款次数, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 38\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:主账户中尚未还清有效贷款\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0          120995\n",
      "800           109\n",
      "400           103\n",
      "30000          85\n",
      "50000          72\n",
      "            ...  \n",
      "353574          1\n",
      "63786           1\n",
      "61739           1\n",
      "359717          1\n",
      "1052556         1\n",
      "Name: 主账户中尚未还清有效贷款, Length: 62763, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 62763\n",
      "------------------------------------\n",
      "是否负值 -22956394\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:主账户中已批准的贷款\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0          117925\n",
      "50000        1264\n",
      "30000        1230\n",
      "100000        816\n",
      "25000         808\n",
      "            ...  \n",
      "155390          1\n",
      "4108000         1\n",
      "5916738         1\n",
      "515790          1\n",
      "1933880         1\n",
      "Name: 主账户中已批准的贷款, Length: 39186, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 39186\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:主账户中已发放贷款\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0         118018\n",
      "50000       1177\n",
      "30000       1135\n",
      "100000       792\n",
      "40000        671\n",
      "           ...  \n",
      "940646         1\n",
      "92800          1\n",
      "383638         1\n",
      "119447         1\n",
      "6183           1\n",
      "Name: 主账户中已发放贷款, Length: 42246, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 42246\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:主账户每月还款\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0        136490\n",
      "1620        239\n",
      "1500        134\n",
      "1600        125\n",
      "2000        118\n",
      "          ...  \n",
      "19906         1\n",
      "16802         1\n",
      "22945         1\n",
      "6924          1\n",
      "13768         1\n",
      "Name: 主账户每月还款, Length: 25705, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 25705\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:主账户无效贷款次数\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0      129277\n",
      "1       26439\n",
      "2       12880\n",
      "3        7682\n",
      "4        5199\n",
      "        ...  \n",
      "78          1\n",
      "77          1\n",
      "75          1\n",
      "451         1\n",
      "249         1\n",
      "Name: 主账户无效贷款次数, Length: 96, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 96\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:主账户还款期数\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0         120667\n",
      "5           3129\n",
      "6           1903\n",
      "3           1690\n",
      "9           1638\n",
      "           ...  \n",
      "195954         1\n",
      "97666          1\n",
      "357763         1\n",
      "124305         1\n",
      "6183           1\n",
      "Name: 主账户还款期数, Length: 16292, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 16292\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "firstinfo=feature.变量名[32:40]\n",
    "baseinfo(credit_data[firstinfo])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a95c4735",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 次贷款信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5952fdbf",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------------\n",
      "列名:次账户贷款次数\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0     194668\n",
      "1       2965\n",
      "2        917\n",
      "3        371\n",
      "4        254\n",
      "5        128\n",
      "6        104\n",
      "7         63\n",
      "8         61\n",
      "9         32\n",
      "10        31\n",
      "11        26\n",
      "13        13\n",
      "12        12\n",
      "14        10\n",
      "15         8\n",
      "16         7\n",
      "19         5\n",
      "18         5\n",
      "17         5\n",
      "22         4\n",
      "23         4\n",
      "31         4\n",
      "20         3\n",
      "38         2\n",
      "34         2\n",
      "30         2\n",
      "21         2\n",
      "29         1\n",
      "28         1\n",
      "25         1\n",
      "24         1\n",
      "35         1\n",
      "37         1\n",
      "42         1\n",
      "46         1\n",
      "52         1\n",
      "Name: 次账户贷款次数, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 37\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:次账户有效贷款次数\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0     196431\n",
      "1       2314\n",
      "2        550\n",
      "3        175\n",
      "4         90\n",
      "5         54\n",
      "6         26\n",
      "7         19\n",
      "8         15\n",
      "9         10\n",
      "11         7\n",
      "10         7\n",
      "12         5\n",
      "15         3\n",
      "13         2\n",
      "16         2\n",
      "22         2\n",
      "14         1\n",
      "17         1\n",
      "20         1\n",
      "21         1\n",
      "36         1\n",
      "Name: 次账户有效贷款次数, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 22\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:次账户中尚未还清有效贷款\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0        196819\n",
      "800           9\n",
      "400           7\n",
      "100           7\n",
      "1200          6\n",
      "          ...  \n",
      "4910          1\n",
      "2989          1\n",
      "37950         1\n",
      "48443         1\n",
      "2047          1\n",
      "Name: 次账户中尚未还清有效贷款, Length: 2805, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 2805\n",
      "------------------------------------\n",
      "是否负值 -1178563\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:次账户中已批准贷款\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0         196505\n",
      "50000         71\n",
      "100000        51\n",
      "30000         39\n",
      "40000         34\n",
      "           ...  \n",
      "14250          1\n",
      "18340          1\n",
      "337800         1\n",
      "26400          1\n",
      "5436           1\n",
      "Name: 次账户中已批准贷款, Length: 1946, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 1946\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:次账户中已发放贷款\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0          196536\n",
      "50000          49\n",
      "100000         40\n",
      "200000         31\n",
      "40000          26\n",
      "            ...  \n",
      "49196           1\n",
      "224990          1\n",
      "34329           1\n",
      "1191500         1\n",
      "42859           1\n",
      "Name: 次账户中已发放贷款, Length: 2232, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 2232\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:次账户每月还款\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0        197784\n",
      "1100          6\n",
      "1065          6\n",
      "1167          5\n",
      "2100          5\n",
      "          ...  \n",
      "428           1\n",
      "31139         1\n",
      "2349          1\n",
      "44            1\n",
      "1919          1\n",
      "Name: 次账户每月还款, Length: 1690, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 1690\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:次账户无效贷款次数\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0     196732\n",
      "1       1848\n",
      "2        518\n",
      "3        200\n",
      "4        127\n",
      "5         88\n",
      "6         63\n",
      "7         42\n",
      "8         22\n",
      "9         17\n",
      "10        13\n",
      "13         9\n",
      "11         6\n",
      "15         6\n",
      "14         5\n",
      "12         4\n",
      "16         4\n",
      "17         2\n",
      "22         2\n",
      "18         1\n",
      "19         1\n",
      "25         1\n",
      "26         1\n",
      "28         1\n",
      "33         1\n",
      "34         1\n",
      "37         1\n",
      "42         1\n",
      "Name: 次账户无效贷款次数, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 28\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "---------------------\n",
      "列名:次账户还款期数\n",
      "---------------------\n",
      "当前数据类型 int64\n",
      "------------------------------------\n",
      "0          196625\n",
      "5              62\n",
      "11             47\n",
      "9              43\n",
      "19             37\n",
      "            ...  \n",
      "61486           1\n",
      "49192           1\n",
      "32800           1\n",
      "38589           1\n",
      "3050000         1\n",
      "Name: 次账户还款期数, Length: 1574, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 1574\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "secondinfo=feature.变量名[40:48]\n",
    "baseinfo(credit_data[secondinfo])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec067b08",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 异常值处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f3db065",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 处理字段数据不匹配"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9799f655",
   "metadata": {
    "hidden": true
   },
   "source": [
    "处理贷款总次数为0，但平均贷款期限&第一次贷款距今时间不为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7f802654",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "cond=(credit_data.贷款总次数!=0)&((credit_data.平均贷款期限==0)|(credit_data.第一次贷款距今时间==0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ef2a0c0c",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "loan_data=credit_data[~cond]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "bfee0657",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(196667, 49)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loan_data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa60d373",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 处理数值<0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "afaae6f9",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "negvalue_col=[\"尚未还清有效贷款总额\",\"主账户中尚未还清有效贷款\",\"次账户中尚未还清有效贷款\"]\n",
    "for col in negvalue_col:\n",
    "    loan_data=loan_data[loan_data[col]>=0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a0e62172",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(196220, 49)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loan_data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2b907d9",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 处理比例异常值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f982a004",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 处理比例<1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "191fadc8",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "loan_data=loan_data[loan_data[\"贷款与已还贷款比列\"]>=1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "6d7f5547",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "loan_data=loan_data[loan_data[\"贷款与已批准贷款比列\"]>=1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b8e80de1",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(183376, 49)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loan_data=loan_data[loan_data[\"总贷款次数与总有效贷款次数比\"]>=1]\n",
    "loan_data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a5f5086",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 处理无限数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d509781",
   "metadata": {
    "hidden": true
   },
   "source": [
    "贷款与已还贷款比列 中有无限值，需删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "83047390",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "cond=np.isinf(loan_data[\"贷款与已还贷款比列\"])\n",
    "loan_data=loan_data[~cond]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c14be1b8",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(183376, 49)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loan_data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43d90c1b",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 处理还款期数极端值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d6283ed",
   "metadata": {
    "hidden": true
   },
   "source": [
    "以50年为最大还款年限，其对应还款期数<=600"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8fa74301",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "problem_col=[\"主账户还款期数\",\"次账户还款期数\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8b095fa5",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "for col in problem_col:\n",
    "    loan_data=loan_data[loan_data[col]<=600]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2cecff72",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(161705, 49)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loan_data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b272480",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 缺失值处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afb24c27",
   "metadata": {
    "hidden": true
   },
   "source": [
    "认为信用评分中0为缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "89aa7067",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "def navalue(data):\n",
    "    med_value=data[data[\"信用评分\"]!=0][\"信用评分\"].median()\n",
    "    data[\"信用评分\"]=data[\"信用评分\"].replace(0,med_value)\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "12121750",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "load_data=navalue(loan_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "66c1003b",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(161705, 49)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load_data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b71e67b",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 特征衍生"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3752836d",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 计算年龄"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "442d1092",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "load_data[\"年龄\"]=load_data[\"货款日期\"]-load_data[\"出生日期\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e94001e3",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 各品牌对应违约率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "6f8c8151",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "brand_rate=dict(zip(load_data.groupby(\"品牌\")[\"是否违约\"].mean().index,\n",
    "    load_data.groupby(\"品牌\")[\"是否违约\"].mean()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "e70f6d50",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "load_data[\"品牌违约率\"]=load_data[\"品牌\"].map(brand_rate)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "176b4ef5",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 各销售商对应违约率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "4299d86e",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "sale_rate=dict(zip(load_data.groupby(\"汽车销售商\")[\"是否违约\"].mean().index,\n",
    "                   load_data.groupby(\"汽车销售商\")[\"是否违约\"].mean()))\n",
    "\n",
    "load_data[\"销售商违约率\"]=load_data[\"汽车销售商\"].map(sale_rate)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2adcb398",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 各车厂对应违约率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "bd5950c7",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "factory_rate=dict(zip(load_data.groupby(\"车厂\")[\"是否违约\"].mean().index,\n",
    "                   load_data.groupby(\"车厂\")[\"是否违约\"].mean()))\n",
    "\n",
    "load_data[\"车厂违约率\"]=load_data[\"车厂\"].map(factory_rate)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b34481a",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 各地区违约率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "de6c7f60",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "place_rate=dict(zip(load_data.groupby(\"地区\")[\"是否违约\"].mean().index,\n",
    "                   load_data.groupby(\"地区\")[\"是否违约\"].mean()))\n",
    "\n",
    "load_data[\"地区违约率\"]=load_data[\"地区\"].map(place_rate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "e99122c2",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>汽车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "      <th>...</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "      <th>年龄</th>\n",
       "      <th>品牌违约率</th>\n",
       "      <th>销售商违约率</th>\n",
       "      <th>车厂违约率</th>\n",
       "      <th>地区违约率</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601758</td>\n",
       "      <td>65532</td>\n",
       "      <td>78990</td>\n",
       "      <td>84.38</td>\n",
       "      <td>136</td>\n",
       "      <td>20490</td>\n",
       "      <td>45</td>\n",
       "      <td>1981</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>37</td>\n",
       "      <td>0.194742</td>\n",
       "      <td>0.248447</td>\n",
       "      <td>0.191524</td>\n",
       "      <td>0.201052</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>447579</td>\n",
       "      <td>58413</td>\n",
       "      <td>67960</td>\n",
       "      <td>89.02</td>\n",
       "      <td>5</td>\n",
       "      <td>15663</td>\n",
       "      <td>86</td>\n",
       "      <td>1977</td>\n",
       "      <td>2018</td>\n",
       "      <td>9</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "      <td>41</td>\n",
       "      <td>0.193139</td>\n",
       "      <td>0.213115</td>\n",
       "      <td>0.175169</td>\n",
       "      <td>0.186521</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>458210</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450</td>\n",
       "      <td>79.45</td>\n",
       "      <td>146</td>\n",
       "      <td>14181</td>\n",
       "      <td>45</td>\n",
       "      <td>1974</td>\n",
       "      <td>2018</td>\n",
       "      <td>17</td>\n",
       "      <td>...</td>\n",
       "      <td>42</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.06</td>\n",
       "      <td>1</td>\n",
       "      <td>44</td>\n",
       "      <td>0.256243</td>\n",
       "      <td>0.250000</td>\n",
       "      <td>0.191524</td>\n",
       "      <td>0.217005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>616513</td>\n",
       "      <td>63882</td>\n",
       "      <td>79605</td>\n",
       "      <td>82.91</td>\n",
       "      <td>152</td>\n",
       "      <td>14470</td>\n",
       "      <td>51</td>\n",
       "      <td>1993</td>\n",
       "      <td>2018</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.50</td>\n",
       "      <td>0</td>\n",
       "      <td>25</td>\n",
       "      <td>0.111210</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.175972</td>\n",
       "      <td>0.154246</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>453368</td>\n",
       "      <td>54013</td>\n",
       "      <td>62371</td>\n",
       "      <td>89.79</td>\n",
       "      <td>34</td>\n",
       "      <td>16556</td>\n",
       "      <td>86</td>\n",
       "      <td>1971</td>\n",
       "      <td>2018</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1</td>\n",
       "      <td>47</td>\n",
       "      <td>0.150731</td>\n",
       "      <td>0.108696</td>\n",
       "      <td>0.175169</td>\n",
       "      <td>0.173275</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 54 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     客户编号   已发货款   资产成本  贷款与资产比列   品牌  汽车销售商  车厂  出生日期  货款日期  地区  ...  \\\n",
       "0  601758  65532  78990    84.38  136  20490  45  1981  2018   8  ...   \n",
       "2  447579  58413  67960    89.02    5  15663  86  1977  2018   9  ...   \n",
       "4  458210  50078  65450    79.45  146  14181  45  1974  2018  17  ...   \n",
       "5  616513  63882  79605    82.91  152  14470  51  1993  2018   3  ...   \n",
       "6  453368  54013  62371    89.79   34  16556  86  1971  2018   6  ...   \n",
       "\n",
       "   主账户还款期数  次账户还款期数  贷款与已批准贷款比列  总贷款次数与总有效贷款次数比  工作类型  年龄     品牌违约率    销售商违约率  \\\n",
       "0        0        0         1.0            1.00     0  37  0.194742  0.248447   \n",
       "2        0        0         1.0            1.00     1  41  0.193139  0.213115   \n",
       "4       42        0         1.0            1.06     1  44  0.256243  0.250000   \n",
       "5       11        0         1.0            1.50     0  25  0.111210  0.200000   \n",
       "6       31        0         1.0            1.33     1  47  0.150731  0.108696   \n",
       "\n",
       "      车厂违约率     地区违约率  \n",
       "0  0.191524  0.201052  \n",
       "2  0.175169  0.186521  \n",
       "4  0.191524  0.217005  \n",
       "5  0.175972  0.154246  \n",
       "6  0.175169  0.173275  \n",
       "\n",
       "[5 rows x 54 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01e9426c",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 平均贷款间隔"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "7af77d5f",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "load_data[\"平均贷款间隔\"]=np.where(load_data[\"贷款总次数\"]==0,0,\n",
    "                            load_data[\"第一次贷款距今时间\"]/load_data[\"贷款总次数\"]).reshape(-1,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b00e87be",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 特征编码--工作类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "a6e9cede",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "data_midlle=load_data[\"工作类型\"].astype(str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "7c53c2c2",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "data_midlle=pd.get_dummies(data_midlle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "3842bdd0",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "data_midlle.columns=[\"工作类型0\",\"工作类型1\",\"工作类型2\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "15155040",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "load_data=pd.concat([load_data,data_midlle],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "79568f46",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>汽车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "      <th>...</th>\n",
       "      <th>工作类型</th>\n",
       "      <th>年龄</th>\n",
       "      <th>品牌违约率</th>\n",
       "      <th>销售商违约率</th>\n",
       "      <th>车厂违约率</th>\n",
       "      <th>地区违约率</th>\n",
       "      <th>平均贷款间隔</th>\n",
       "      <th>工作类型0</th>\n",
       "      <th>工作类型1</th>\n",
       "      <th>工作类型2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601758</td>\n",
       "      <td>65532</td>\n",
       "      <td>78990</td>\n",
       "      <td>84.38</td>\n",
       "      <td>136</td>\n",
       "      <td>20490</td>\n",
       "      <td>45</td>\n",
       "      <td>1981</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>37</td>\n",
       "      <td>0.194742</td>\n",
       "      <td>0.248447</td>\n",
       "      <td>0.191524</td>\n",
       "      <td>0.201052</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>447579</td>\n",
       "      <td>58413</td>\n",
       "      <td>67960</td>\n",
       "      <td>89.02</td>\n",
       "      <td>5</td>\n",
       "      <td>15663</td>\n",
       "      <td>86</td>\n",
       "      <td>1977</td>\n",
       "      <td>2018</td>\n",
       "      <td>9</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>41</td>\n",
       "      <td>0.193139</td>\n",
       "      <td>0.213115</td>\n",
       "      <td>0.175169</td>\n",
       "      <td>0.186521</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>458210</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450</td>\n",
       "      <td>79.45</td>\n",
       "      <td>146</td>\n",
       "      <td>14181</td>\n",
       "      <td>45</td>\n",
       "      <td>1974</td>\n",
       "      <td>2018</td>\n",
       "      <td>17</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>44</td>\n",
       "      <td>0.256243</td>\n",
       "      <td>0.250000</td>\n",
       "      <td>0.191524</td>\n",
       "      <td>0.217005</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>616513</td>\n",
       "      <td>63882</td>\n",
       "      <td>79605</td>\n",
       "      <td>82.91</td>\n",
       "      <td>152</td>\n",
       "      <td>14470</td>\n",
       "      <td>51</td>\n",
       "      <td>1993</td>\n",
       "      <td>2018</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>25</td>\n",
       "      <td>0.111210</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.175972</td>\n",
       "      <td>0.154246</td>\n",
       "      <td>12.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>453368</td>\n",
       "      <td>54013</td>\n",
       "      <td>62371</td>\n",
       "      <td>89.79</td>\n",
       "      <td>34</td>\n",
       "      <td>16556</td>\n",
       "      <td>86</td>\n",
       "      <td>1971</td>\n",
       "      <td>2018</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>47</td>\n",
       "      <td>0.150731</td>\n",
       "      <td>0.108696</td>\n",
       "      <td>0.175169</td>\n",
       "      <td>0.173275</td>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 58 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     客户编号   已发货款   资产成本  贷款与资产比列   品牌  汽车销售商  车厂  出生日期  货款日期  地区  ...  工作类型  \\\n",
       "0  601758  65532  78990    84.38  136  20490  45  1981  2018   8  ...     0   \n",
       "2  447579  58413  67960    89.02    5  15663  86  1977  2018   9  ...     1   \n",
       "4  458210  50078  65450    79.45  146  14181  45  1974  2018  17  ...     1   \n",
       "5  616513  63882  79605    82.91  152  14470  51  1993  2018   3  ...     0   \n",
       "6  453368  54013  62371    89.79   34  16556  86  1971  2018   6  ...     1   \n",
       "\n",
       "   年龄     品牌违约率    销售商违约率     车厂违约率     地区违约率  平均贷款间隔  工作类型0  工作类型1  工作类型2  \n",
       "0  37  0.194742  0.248447  0.191524  0.201052     0.0      1      0      0  \n",
       "2  41  0.193139  0.213115  0.175169  0.186521     0.0      0      1      0  \n",
       "4  44  0.256243  0.250000  0.191524  0.217005     1.0      0      1      0  \n",
       "5  25  0.111210  0.200000  0.175972  0.154246    12.0      1      0      0  \n",
       "6  47  0.150731  0.108696  0.175169  0.173275    10.0      0      1      0  \n",
       "\n",
       "[5 rows x 58 columns]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad4a75bf",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 删除不必要特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "5c3e25d1",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "drop_col=[\"客户编号\",\"工作类型\",\"出生日期\",\"地区\",\"是否填写手机号\",\"是否填写身份证\",\"货款日期\",\n",
    "          \"对接员工编号\",\"品牌\",\"汽车销售商\",\"车厂\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "306dadb2",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "load_data.drop(columns=drop_col,inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46972333",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 失衡数据判断并处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78892102",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 失衡数据判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "092e5299",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>是否出具驾驶证</th>\n",
       "      <th>是否填写护照</th>\n",
       "      <th>信用评分</th>\n",
       "      <th>主账户贷款次数</th>\n",
       "      <th>主账户有效贷款次数</th>\n",
       "      <th>主账户中尚未还清有效贷款</th>\n",
       "      <th>主账户中已批准的贷款</th>\n",
       "      <th>...</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>年龄</th>\n",
       "      <th>品牌违约率</th>\n",
       "      <th>销售商违约率</th>\n",
       "      <th>车厂违约率</th>\n",
       "      <th>地区违约率</th>\n",
       "      <th>平均贷款间隔</th>\n",
       "      <th>工作类型0</th>\n",
       "      <th>工作类型1</th>\n",
       "      <th>工作类型2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>65532</td>\n",
       "      <td>78990</td>\n",
       "      <td>84.38</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>692.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.00</td>\n",
       "      <td>37</td>\n",
       "      <td>0.194742</td>\n",
       "      <td>0.248447</td>\n",
       "      <td>0.191524</td>\n",
       "      <td>0.201052</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>58413</td>\n",
       "      <td>67960</td>\n",
       "      <td>89.02</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>692.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.00</td>\n",
       "      <td>41</td>\n",
       "      <td>0.193139</td>\n",
       "      <td>0.213115</td>\n",
       "      <td>0.175169</td>\n",
       "      <td>0.186521</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>50078</td>\n",
       "      <td>65450</td>\n",
       "      <td>79.45</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>379.0</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>467161</td>\n",
       "      <td>550000</td>\n",
       "      <td>...</td>\n",
       "      <td>1.06</td>\n",
       "      <td>44</td>\n",
       "      <td>0.256243</td>\n",
       "      <td>0.250000</td>\n",
       "      <td>0.191524</td>\n",
       "      <td>0.217005</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>63882</td>\n",
       "      <td>79605</td>\n",
       "      <td>82.91</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>749.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>16225</td>\n",
       "      <td>17700</td>\n",
       "      <td>...</td>\n",
       "      <td>1.50</td>\n",
       "      <td>25</td>\n",
       "      <td>0.111210</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.175972</td>\n",
       "      <td>0.154246</td>\n",
       "      <td>12.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>54013</td>\n",
       "      <td>62371</td>\n",
       "      <td>89.79</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>300.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>12991</td>\n",
       "      <td>100000</td>\n",
       "      <td>...</td>\n",
       "      <td>1.33</td>\n",
       "      <td>47</td>\n",
       "      <td>0.150731</td>\n",
       "      <td>0.108696</td>\n",
       "      <td>0.175169</td>\n",
       "      <td>0.173275</td>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 47 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    已发货款   资产成本  贷款与资产比列  是否出具驾驶证  是否填写护照   信用评分  主账户贷款次数  主账户有效贷款次数  \\\n",
       "0  65532  78990    84.38        0       0  692.0        0          0   \n",
       "2  58413  67960    89.02        0       0  692.0        0          0   \n",
       "4  50078  65450    79.45        0       0  379.0        8          1   \n",
       "5  63882  79605    82.91        0       0  749.0        2          1   \n",
       "6  54013  62371    89.79        0       0  300.0        3          1   \n",
       "\n",
       "   主账户中尚未还清有效贷款  主账户中已批准的贷款  ...  总贷款次数与总有效贷款次数比  年龄     品牌违约率    销售商违约率  \\\n",
       "0             0           0  ...            1.00  37  0.194742  0.248447   \n",
       "2             0           0  ...            1.00  41  0.193139  0.213115   \n",
       "4        467161      550000  ...            1.06  44  0.256243  0.250000   \n",
       "5         16225       17700  ...            1.50  25  0.111210  0.200000   \n",
       "6         12991      100000  ...            1.33  47  0.150731  0.108696   \n",
       "\n",
       "      车厂违约率     地区违约率  平均贷款间隔  工作类型0  工作类型1  工作类型2  \n",
       "0  0.191524  0.201052     0.0      1      0      0  \n",
       "2  0.175169  0.186521     0.0      0      1      0  \n",
       "4  0.191524  0.217005     1.0      0      1      0  \n",
       "5  0.175972  0.154246    12.0      1      0      0  \n",
       "6  0.175169  0.173275    10.0      0      1      0  \n",
       "\n",
       "[5 rows x 47 columns]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "d361dddb",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    131874\n",
       "1     29831\n",
       "Name: 是否违约, dtype: int64"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load_data[\"是否违约\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "baa42bf6",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.815522\n",
       "1    0.184478\n",
       "Name: 是否违约, dtype: float64"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load_data[\"是否违约\"].value_counts()/load_data.shape[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28562d48",
   "metadata": {
    "hidden": true
   },
   "source": [
    "由于1仅占18.4%，所以此数据为失衡数据，且1的数据量达到29000，可以使用欠采样"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe442917",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 欠采样取数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "24652ddb",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "y_sample=load_data.pop(\"是否违约\")\n",
    "x_sample=load_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "f6295685",
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from imblearn.under_sampling import NearMiss\n",
    "nm=NearMiss(version=1)\n",
    "x_resample,y_resample=nm.fit_resample(x_sample,y_sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "5f8be89d",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "load_data=pd.concat([x_resample,y_resample],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "9ce06009",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    29831\n",
       "1    29831\n",
       "Name: 是否违约, dtype: int64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load_data[\"是否违约\"].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20ac80df",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 划分数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "4ea20173",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "load_data.reset_index(drop=True,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "d772532c",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>是否出具驾驶证</th>\n",
       "      <th>是否填写护照</th>\n",
       "      <th>信用评分</th>\n",
       "      <th>主账户贷款次数</th>\n",
       "      <th>主账户有效贷款次数</th>\n",
       "      <th>主账户中尚未还清有效贷款</th>\n",
       "      <th>主账户中已批准的贷款</th>\n",
       "      <th>...</th>\n",
       "      <th>年龄</th>\n",
       "      <th>品牌违约率</th>\n",
       "      <th>销售商违约率</th>\n",
       "      <th>车厂违约率</th>\n",
       "      <th>地区违约率</th>\n",
       "      <th>平均贷款间隔</th>\n",
       "      <th>工作类型0</th>\n",
       "      <th>工作类型1</th>\n",
       "      <th>工作类型2</th>\n",
       "      <th>是否违约</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>48478</td>\n",
       "      <td>67320</td>\n",
       "      <td>74.87</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>692.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>26</td>\n",
       "      <td>0.229603</td>\n",
       "      <td>0.344186</td>\n",
       "      <td>0.230855</td>\n",
       "      <td>0.154246</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>44745</td>\n",
       "      <td>62320</td>\n",
       "      <td>74.78</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>692.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>31</td>\n",
       "      <td>0.229603</td>\n",
       "      <td>0.344186</td>\n",
       "      <td>0.230855</td>\n",
       "      <td>0.154246</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>44745</td>\n",
       "      <td>62320</td>\n",
       "      <td>74.78</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>692.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>38</td>\n",
       "      <td>0.229603</td>\n",
       "      <td>0.344186</td>\n",
       "      <td>0.230855</td>\n",
       "      <td>0.154246</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>52478</td>\n",
       "      <td>68930</td>\n",
       "      <td>79.79</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>692.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>25</td>\n",
       "      <td>0.225043</td>\n",
       "      <td>0.307692</td>\n",
       "      <td>0.230855</td>\n",
       "      <td>0.173275</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>44745</td>\n",
       "      <td>62320</td>\n",
       "      <td>74.78</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>692.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>28</td>\n",
       "      <td>0.229603</td>\n",
       "      <td>0.344186</td>\n",
       "      <td>0.230855</td>\n",
       "      <td>0.154246</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 47 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    已发货款   资产成本  贷款与资产比列  是否出具驾驶证  是否填写护照   信用评分  主账户贷款次数  主账户有效贷款次数  \\\n",
       "0  48478  67320    74.87        0       0  692.0        0          0   \n",
       "1  44745  62320    74.78        0       0  692.0        0          0   \n",
       "2  44745  62320    74.78        0       0  692.0        0          0   \n",
       "3  52478  68930    79.79        0       0  692.0        0          0   \n",
       "4  44745  62320    74.78        0       0  692.0        0          0   \n",
       "\n",
       "   主账户中尚未还清有效贷款  主账户中已批准的贷款  ...  年龄     品牌违约率    销售商违约率     车厂违约率     地区违约率  \\\n",
       "0             0           0  ...  26  0.229603  0.344186  0.230855  0.154246   \n",
       "1             0           0  ...  31  0.229603  0.344186  0.230855  0.154246   \n",
       "2             0           0  ...  38  0.229603  0.344186  0.230855  0.154246   \n",
       "3             0           0  ...  25  0.225043  0.307692  0.230855  0.173275   \n",
       "4             0           0  ...  28  0.229603  0.344186  0.230855  0.154246   \n",
       "\n",
       "   平均贷款间隔  工作类型0  工作类型1  工作类型2  是否违约  \n",
       "0     0.0      1      0      0     0  \n",
       "1     0.0      1      0      0     0  \n",
       "2     0.0      1      0      0     0  \n",
       "3     0.0      0      1      0     0  \n",
       "4     0.0      1      0      0     0  \n",
       "\n",
       "[5 rows x 47 columns]"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "63e2e213",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "xtrain,xtest,ytrain,ytest=train_test_split(load_data.iloc[:,:-1],\n",
    "                                           load_data.iloc[:,-1],test_size=0.2,random_state=420)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6561243",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 多个备选模型并计较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "21f8507a",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 常规模型\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "#集成模型\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "#模型评估\n",
    "from sklearn import metrics\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "c038c954",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "#定义模型函数\n",
    "def model_judge(xtrain,ytrain,xtest,ytest,model_name,model):\n",
    "    print(\"训练{}模型\".format(model_name))\n",
    "    start=time.time()\n",
    "    clf=model\n",
    "    clf=clf.fit(xtrain,ytrain)\n",
    "    #验证训练集拟合度\n",
    "    trainscore=clf.score(xtrain,ytrain)\n",
    "    print(\"训练集拟合度:{:.2f}\".format(trainscore))\n",
    "    #验证测试机拟合度\n",
    "    y_pred=clf.predict(xtest)\n",
    "    testscore=clf.score(xtest,ytest)\n",
    "    accuracy=metrics.accuracy_score(ytest,y_pred)\n",
    "    precision=metrics.precision_score(ytest,y_pred)\n",
    "    recall=metrics.recall_score(ytest,y_pred)\n",
    "    print(\"测试集R方:{:.2f}\".format(testscore))\n",
    "    print(\"测试集准确度:{:.2f}\".format(accuracy))\n",
    "    print(\"测试集精准率:{:.2f}\".format(precision))\n",
    "    print(\"测试集召回率:{:.2f}\".format(recall))\n",
    "    end=time.time()\n",
    "    duration=end-start\n",
    "    print(\"模型耗时:{:6f}s\".format(duration))\n",
    "          \n",
    "    return model_name,trainscore,testscore,accuracy,precision,recall,duration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "8d7c5a62",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练KNN模型\n",
      "训练集拟合度:0.80\n",
      "测试集R方:0.73\n",
      "测试集准确度:0.73\n",
      "测试集精准率:0.79\n",
      "测试集召回率:0.61\n",
      "模型耗时:62.169237s\n",
      "训练决策树模型\n",
      "训练集拟合度:1.00\n",
      "测试集R方:0.69\n",
      "测试集准确度:0.69\n",
      "测试集精准率:0.69\n",
      "测试集召回率:0.70\n",
      "模型耗时:0.559431s\n",
      "训练逻辑回归模型\n",
      "训练集拟合度:0.75\n",
      "测试集R方:0.75\n",
      "测试集准确度:0.75\n",
      "测试集精准率:0.89\n",
      "测试集召回率:0.56\n",
      "模型耗时:0.422935s\n",
      "训练随机森林模型\n",
      "训练集拟合度:1.00\n",
      "测试集R方:0.77\n",
      "测试集准确度:0.77\n",
      "测试集精准率:0.83\n",
      "测试集召回率:0.67\n",
      "模型耗时:7.182117s\n"
     ]
    }
   ],
   "source": [
    "model_param={\n",
    "    \"KNN\":KNeighborsClassifier(),\n",
    "    \"决策树\":DecisionTreeClassifier(),\n",
    "    \"逻辑回归\":LogisticRegression(),\n",
    "    \"随机森林\":RandomForestClassifier()    \n",
    "}\n",
    "result_df=pd.DataFrame(columns=[\"模型名称\",\"训练集拟合度\",\"测试集R方\",\n",
    "                            \"测试集准确度\",\"测试集精准率\",\"测试集召回率\",\"模型耗时\"])\n",
    "for model_name,model in model_param.items():\n",
    "    result=model_judge(xtrain,ytrain,xtest,\n",
    "                       ytest,model_name,model)\n",
    "    result_df.loc[result_df.shape[0],:]=result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "0371f817",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>模型名称</th>\n",
       "      <th>训练集拟合度</th>\n",
       "      <th>测试集R方</th>\n",
       "      <th>测试集准确度</th>\n",
       "      <th>测试集精准率</th>\n",
       "      <th>测试集召回率</th>\n",
       "      <th>模型耗时</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>KNN</td>\n",
       "      <td>0.801881</td>\n",
       "      <td>0.726976</td>\n",
       "      <td>0.726976</td>\n",
       "      <td>0.79218</td>\n",
       "      <td>0.614959</td>\n",
       "      <td>62.169237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>决策树</td>\n",
       "      <td>0.998806</td>\n",
       "      <td>0.694712</td>\n",
       "      <td>0.694712</td>\n",
       "      <td>0.691989</td>\n",
       "      <td>0.701157</td>\n",
       "      <td>0.559431</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>逻辑回归</td>\n",
       "      <td>0.747533</td>\n",
       "      <td>0.748345</td>\n",
       "      <td>0.748345</td>\n",
       "      <td>0.892781</td>\n",
       "      <td>0.564146</td>\n",
       "      <td>0.422935</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>随机森林</td>\n",
       "      <td>0.998806</td>\n",
       "      <td>0.766949</td>\n",
       "      <td>0.766949</td>\n",
       "      <td>0.827501</td>\n",
       "      <td>0.674157</td>\n",
       "      <td>7.182117</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   模型名称    训练集拟合度     测试集R方    测试集准确度    测试集精准率    测试集召回率       模型耗时\n",
       "0   KNN  0.801881  0.726976  0.726976   0.79218  0.614959  62.169237\n",
       "1   决策树  0.998806  0.694712  0.694712  0.691989  0.701157   0.559431\n",
       "2  逻辑回归  0.747533  0.748345  0.748345  0.892781  0.564146   0.422935\n",
       "3  随机森林  0.998806  0.766949  0.766949  0.827501  0.674157   7.182117"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result_df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d126030",
   "metadata": {
    "hidden": true
   },
   "source": [
    "通过比较四个模型，发现本案例中使用随机森林更为妥当（训练集和测试集分数都高）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17d05b70",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 使用网格搜索调优"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d42edc66",
   "metadata": {
    "hidden": true
   },
   "source": [
    "由于测试模型中随机森林的拟合度和测试集很高了，所以在其n_estimators等参数附近调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "622e6dba",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "clf=RandomForestClassifier().fit(xtrain,ytrain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "c1c25b15",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf.n_estimators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "10cf4ec5",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'gini'"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf.criterion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "84215eaf",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "p={\"n_estimators\":np.arange(100,150,10),\n",
    "  \"max_depth\":list(np.linspace(4,20,9))\n",
    "  }\n",
    "\n",
    "model=RandomForestClassifier()\n",
    "gridsearch=GridSearchCV(model,p,cv=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "6ee79e4c",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, estimator=RandomForestClassifier(),\n",
       "             param_grid={'max_depth': [4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0,\n",
       "                                       18.0, 20.0],\n",
       "                         'n_estimators': array([100, 110, 120, 130, 140])})"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gridsearch.fit(xtrain,ytrain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "caedea67",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'max_depth': 16.0, 'n_estimators': 140}"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gridsearch.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "a03b9440",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 重新建立模型\n",
    "clf=RandomForestClassifier(n_estimators=gridsearch.best_params_[\"n_estimators\"],\n",
    "                      max_depth=gridsearch.best_params_[\"max_depth\"])\n",
    "clf=clf.fit(xtrain,ytrain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "6ba22d88",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8386725051855266"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#训练集拟合度\n",
    "clf.score(xtrain,ytrain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "e9616bd7",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7784295650716501"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#测试集拟合度\n",
    "clf.score(xtest,ytest) #调优前 0.766949"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "39729a9d",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "y_pre=clf.predict(xtest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "da0a5c6f",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7784295650716501"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#测试集准确度\n",
    "metrics.accuracy_score(ytest,y_pre) #调优前 0.766949"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "0ee19934",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8836994219653179"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#测试集精准率\n",
    "metrics.precision_score(ytest,y_pre)#调优前0.827501"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "9b975c01",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6409525406674492"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#测试集召回率\n",
    "metrics.recall_score(ytest,y_pre) #调优前0.674157"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f06f57a8",
   "metadata": {
    "hidden": true
   },
   "source": [
    "虽然训练集拟合度和测试集召回率有所下滑，但整体模型较之前有所提升"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42f7f337",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 保存模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "2ef0afa0",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "import joblib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "f16cb2db",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['车贷违约预测模型']"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#保存模型\n",
    "joblib.dump(clf,\"车贷违约预测模型\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "613ba479",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RandomForestClassifier(max_depth=16.0, n_estimators=140)"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#调用模型\n",
    "joblib.load(\"车贷违约预测模型\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
